package com.xmutca.sso.provider.manager;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.xmutca.core.common.Paging;
import com.xmutca.core.common.Receipt;
import com.xmutca.core.common.Result;
import com.xmutca.core.common.Results;
import com.xmutca.sso.api.Role;
import com.xmutca.sso.api.manager.service.RoleService;
import com.xmutca.sso.dao.RoleDao;
import com.xmutca.sso.dao.RolePermissionDao;
import com.xmutca.sso.dao.UserRoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Map;

/**
 * @author: 彭伟煌(pengweihuang@xmutca.com)
 * @create: 2016-08-29 16:24
 */
@Service(version = "1.0.0", group = "sso", mock = "true")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    @Transactional
    public Receipt add(Role role, List<Integer> permissions) {
        roleDao.add(role);

        //批量删除旧权限数据，并新增新权限数据
        batchDeleteAndSavePermission(role.getId(), permissions);
        return new Receipt(Receipt.Status.SUCCESS);
    }

    @Override
    @Transactional
    public Receipt del(Integer id) {
        roleDao.updateState(Role.STATE_UNABLE, id);

        //角色权限，用户角色
        rolePermissionDao.delByRole(id);
        userRoleDao.delByRole(id);
        return new Receipt(Receipt.Status.SUCCESS);
    }

    @Override
    @Transactional
    public Receipt batchDel(List<Integer> ids) {
        roleDao.batchUpdateState(Role.STATE_UNABLE, ids);

        //角色权限，用户角色
        rolePermissionDao.batchDelByRole(ids);
        userRoleDao.batchDelByRole(ids);
        return new Receipt(Receipt.Status.SUCCESS);
    }

    @Override
    @Transactional
    public Receipt update(Role role, List<Integer> permissions) {
        roleDao.update(role);

        //批量删除旧权限数据，并新增新权限数据
        batchDeleteAndSavePermission(role.getId(), permissions);
        return new Receipt(Receipt.Status.SUCCESS);
    }

    @Override
    public Result<Role> get(Integer id) {
        return new Result<>(Receipt.Status.SUCCESS, roleDao.get(id));
    }

    @Override
    public Results<Role> query(@Min(value = 1, message = "页数最小为1") int current, @Max(value = 50, message = "条数最大为50条") int pageSize, Map<String, Object> paramMap) {
        int count = roleDao.countQuery(paramMap);
        paramMap.put("startRow", (current - 1) * pageSize);
        paramMap.put("pageSize", pageSize);
        List<Role> roles = roleDao.query(paramMap);
        return new Results<>(Results.Status.SUCCESS, roles, new Paging(current, pageSize, count));
    }

    /**
     * 批量删除旧权限数据，并新增新权限数据
     * @param role
     * @param permissions
     */
    private void batchDeleteAndSavePermission(Integer role, List<Integer> permissions) {
        //删除旧权限数据
        rolePermissionDao.delByRole(role);

        //新增新权限数据
        if (CollectionUtils.isNotEmpty(permissions)) {
            for (Integer permission : permissions) {
                rolePermissionDao.add(role, permission);
            }
        }
    }
}
